﻿using System.Drawing;
using XFControls.Utils;

namespace XFControls.XFPanels.XFPanelItems
{
    public class XFItemDisplayHead : XFItemBase
    {
        private int _height;
        private int _imageHeight = 128;
        private int _imageWidth = 128;
        private string _status;
        private string _pictureUrl;

        public XFItemDisplayHead(string status, string pictureUrl)
        {
            _height = -1;
            Style = new XFItemStyle
                        {
                            TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                            Padding = 10,
                            BackgroundColor = Color.RoyalBlue,
                            TextColor = Color.White,
                            SelectedTextColor = Color.Black
                        };

            ItemType = XFItemType.Display;
            _status = status;
            _pictureUrl = pictureUrl;
        }

        public override int GetHeight()
        {
            if (_height > 0)
                return _height;

            int width = GetWidth();

            width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;
            _height = Style.TextFont.MeasureString(_status, new Rectangle(0, 0, width, 0)).Height + Style.PaddingBottom;


            if (_height < _imageHeight + Style.PaddingTop)
                _height = _imageHeight + Style.PaddingTop;

            _height += Style.PaddingBottom;
            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);
            int width = GetWidth(false);

            DrawImage(_pictureUrl, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _imageWidth, _imageHeight));

            Color textColor = getCurrentFontColor(false);

            using (var p = new Pen(Color.DarkGray))
                g.DrawLine(p, 0, y + _height, width, y + _height);
            using (var b = new SolidBrush(textColor))
            {
                width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;

                g.DrawString(_status,
                             new Rectangle(x + (Style.PaddingLeft * 2) + _imageWidth, y + Style.PaddingRight, width, 0),
                             Style, textColor);
            }
        }
    }
}